package cn.zcy.untils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    // 使用 Base64 编码的公钥和私钥字符串 PUBLIC_KEY_STRING
    private static final String PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJu/gmcsUfyBHh78ryXZ6IPsNY4Fxh4YWzIH6ezDyG5XKhhOpLOEfAXf7SpvKf2njmIBVpLD0cuHXn2sN26TU+0CAwEAAQ=="; // 替换为你的公钥
    private static final String PRIVATE_KEY_STRING = "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAm7+CZyxR/IEeHvyvJdnog+w1jgXGHhhbMgfp7MPIblcqGE6ks4R8Bd/tKm8p/aeOYgFWksPRy4defaw3bpNT7QIDAQABAkBbeACEyVGpIsdm2W2fpDda6umU7C0rVZCRdHZcl6rpqZqhfa4f3eG7WXTVcYarrtiwv00I8tTkVXjg9KA2efcdAiEAzo1Ma9l/QWCl1ooXRS63Hh6aqZfGibSxzrfaLg5/JWsCIQDBCKfH6poZnR/aySPEZTo5myNF+/D/b700DJRrFpFqBwIhAIuShWp3TPtjkIx5QaI5H/oVWhNxgPI6jNAQAjmPCDHDAiEAjGJtEvlNJP+eYRETEAvGcoqe9pCpzjtHUIktj0wUaRUCIQCqwnuSJVzVs8ekYsCHFIlefIvOjN5xFtAjvQEyfCnnsA==";   // 替换为你的私钥
//    private static final String PUBLIC_KEY_STRING = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAonmieHU3OeAG5/2X8BCzXXUyZw3IZp3zDfwIDVmO6J1biAkMDCpvcB6+Hsv30D58/g3Zzq11YpZcNpe8J3mGbdy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CB7LQqWLto9tif6JgMra9NWLaRYQSoWkiFopblbuHKLtqs0dCbr5bSohDi5co2SYAN+XJmrYwh62FJXlN87qEnj1TCaGCoETQPdjyhx3rKBBELUiwjwdMolUCgYEAycasrCDpgTgYYnCyeDeOzTMqiitTUNIpQPJwdGCvd2NiRPJqgbOLI5NHy/Hj1kyj+ROy5YA0gGucY0V4ZQr0LrzSoq17Ly8Ow1Y5B7ZkBb0qPmYGAD0XRplZkPkDFFfMjcAzMG9GhLsDk0b79NEh4outWLiIjON/5aDGC1Ve9GECgYBfon3/xPzTTWHHOkrNp3IxdllRlmaRL4IiGEeUej5ja4a91A9NJun3MmGWRFxmCdEvz+B7hsZPb1Y+rCCMnB8BgB0vYXo6ToEZPbU3db8a3A8iQvXjkLrHNCeNTHtFZx5/nUCqE92VIHRW4oSc4HAQUYdrYlkIFEvdCn1HTiaipQKBgGQer/ltD1BuDDN0Zioerz4EwHv0yY+V6KBwa/pjK03bQ5ygiNa4xzas0vhQm1PvjYF/E8qrFeIPA3eCth21FLrIOOA9op0kBXhJpVy9VN5AIeD6cMnXhmAioX29AZkrg7VmhD/pRx1/SmPe2hB9YQnvcuFOF3uJOyIu53YiM06hAoGAI0SI/wRdWM/3Jg5fY+iELqatwl+kD2kmURyy5P31F159L5oKAkyiLZtXxCPgyN4SREY/64IsaIRmT6wsyPI84bOMos934N7N8pUQz1wifAMesP6/BR3/o+WZ7SNJ1YkZuQBSumTC4BXePSiYoTlkWeM9UBhTdvjyq0DboDgj3v8=";   // 替换为你的公钥

    // 生成 JWT
    public String generateToken(Map<String, Object> claims) throws Exception {
        PrivateKey privateKey = getPrivateKey(PRIVATE_KEY_STRING);
        return Jwts.builder()
                .setClaims(claims)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 86400000)) // 1天有效期
                .signWith(SignatureAlgorithm.RS256, privateKey) // 使用私钥签名
                .compact();
    }

    // 验证 JWT
    public Claims validateToken(String token) throws Exception {
        PublicKey publicKey = getPublicKey(PUBLIC_KEY_STRING);
        return Jwts.parser()
                .setSigningKey(publicKey) // 使用公钥验证
                .parseClaimsJws(token)
                .getBody();
    }

    // 从 Base64 字符串获取私钥
    private PrivateKey getPrivateKey(String base64PrivateKey) throws Exception {
        byte[] bytes = Base64.getDecoder().decode(base64PrivateKey);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(spec);
    }

    // 从 Base64 字符串获取公钥
    private PublicKey getPublicKey(String base64PublicKey) throws Exception {
        byte[] bytes = Base64.getDecoder().decode(base64PublicKey);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(spec);
    }

}

